package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.CouponActivity;
import com.example.mall.domain.CouponProductRelation;
import com.example.mall.dto.CouponActivityDto;
import com.example.mall.dto.CouponProductRelationDto;
import com.example.mall.mapper.CouponProductRelationMapper;
import com.example.mall.query.CouponActivityQuery;
import com.example.mall.query.CouponProductRelationQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ICouponProductRelationService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import com.example.mall.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券适用商品关联表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CouponProductRelationServiceImpl extends ServiceImpl<CouponProductRelationMapper, CouponProductRelation> implements ICouponProductRelationService {

    @Autowired
    private CouponProductRelationMapper couponProductRelationMapper;

    @Override
    public PageDTO<CouponProductRelationDto> queryPage(CouponProductRelationQuery couponProductRelationQuery) {
        if (couponProductRelationQuery == null) {
            couponProductRelationQuery = new CouponProductRelationQuery();
        }
        Page<CouponProductRelation> page = couponProductRelationQuery.toMpPageByDefaultCreateTimeAsc();
        Page<CouponProductRelation> cartPage = lambdaQuery()
                .like(couponProductRelationQuery.getRelationId()!= null, CouponProductRelation::getRelationId, couponProductRelationQuery.getRelationId())
                .like(couponProductRelationQuery.getTemplateId()!= null, CouponProductRelation::getTemplateId, couponProductRelationQuery.getTemplateId())
                .like(couponProductRelationQuery.getProductId()!= null, CouponProductRelation::getProductId, couponProductRelationQuery.getProductId())
                .like(couponProductRelationQuery.getStatus()!= null, CouponProductRelation::getStatus, couponProductRelationQuery.getStatus())
                .orderByDesc(CouponProductRelation::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, CouponProductRelationDto.class);
    }

    @Override
    public void insert(CouponProductRelationDto couponProductRelationDto) {
        CouponProductRelation couponProductRelation = new CouponProductRelation();
        CopyUtils.copyProperties(couponProductRelationDto, couponProductRelation);
        couponProductRelation.setRelationId(CodeGenerateUtils.generateUniqueId());
        couponProductRelation.setCreateDate(new Date());
        couponProductRelation.setUpdateDate(new Date());
        couponProductRelation.setStatus(BaseConstants.EFFECTIVE_STATUS);
        couponProductRelationMapper.insert(couponProductRelation);
    }

    @Override
    public void update(CouponProductRelationDto couponProductRelationDto) {
        AssertUtil.isNotNull(couponProductRelationDto.getRelationId(), "relationId不能为空");
        CouponProductRelation couponProductRelation = couponProductRelationMapper.selectById(couponProductRelationDto.getRelationId());
        AssertUtil.isNotNull(couponProductRelation, "未找到对应优惠券适用商品关联表信息");
        CopyUtils.copyProperties(couponProductRelationDto, couponProductRelation);
        couponProductRelation.setUpdateDate(new Date());
        couponProductRelationMapper.updateById(couponProductRelation);
    }

    @Override
    public void delete(Long relationId) {
        AssertUtil.isNotNull(relationId, "relationId不能为空");
        CouponProductRelation couponProductRelation = couponProductRelationMapper.selectById(relationId);
        AssertUtil.isNotNull(couponProductRelation, "未找到对应优惠券适用商品关联表信息");
        couponProductRelation.setStatus(BaseConstants.LOSE_STATUS);
        couponProductRelation.setUpdateDate(new Date());
        couponProductRelationMapper.updateById(couponProductRelation);
    }

    @Override
    public void deleteBatch(List<Long> relationIds) {
        AssertUtil.isNotNull(relationIds, "relationIds不能为空");
        List<CouponProductRelation> couponProductRelations = couponProductRelationMapper.selectBatchIds(relationIds);
        AssertUtil.isNotNull(couponProductRelations, "未找到对应优惠券适用商品关联表信息");
        for (CouponProductRelation couponProductRelation : couponProductRelations) {
            couponProductRelation.setStatus(BaseConstants.LOSE_STATUS);
            couponProductRelation.setUpdateDate(new Date());
            couponProductRelationMapper.updateById(couponProductRelation);
        }
    }

    @Override
    public CouponProductRelation queryCouponProductRelationById(Long relationId) {
        AssertUtil.isNotNull(relationId, "relationId不能为空");
        return couponProductRelationMapper.selectById(relationId);
    }

    @Override
    public List<CouponProductRelation> queryCouponProductRelationList() {
        return this.list();
    }
}
